package com.sx.micowell.module.trainplan

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.mvvm.until.GridItemDecoration
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils

import com.miloyu.modelnet.response.DictType
import com.miloyu.modelnet.response.getResId

import com.sx.micowell.R
import java.util.*

/**
 * @Author: YY
 * @Time: 2021/8/13
 * @Describe TODO
 */
class TrainTypeListAdapter : BaseQuickAdapter<DictType, BaseViewHolder>(R.layout.item_select_train_time) {

    private var weekListAdapter: WeekListAdapter? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: BaseViewHolder, item: DictType) {

        val recyclerContain: RecyclerView = holder.getView(R.id.recycler_week)
        recyclerContain.isNestedScrollingEnabled = false
        recyclerContain.layoutManager = GridLayoutManager(context, 5)
        if (recyclerContain.itemDecorationCount == 0) {
            //添加分割线
            recyclerContain.addItemDecoration(GridItemDecoration(13, 22))
        }

        weekListAdapter = WeekListAdapter()
        weekListAdapter?.data = item.weeks as MutableList<DictType>
        recyclerContain.adapter = weekListAdapter

        weekListAdapter?.addChildClickViewIds(R.id.tv_week)
        weekListAdapter?.setOnItemChildClickListener { adapter, view, position ->
            LogUtils.d("weekListAdapter: "+view.id)
            if (item.isSelect) {
                val dictType: DictType = item.weeks[position]
                dictType.isSelect = !dictType.isSelect
                adapter.notifyItemChanged(position)

                mOnNestChildClickListener?.onItemClickListener(position, item.weeks)
            } else {
                ToastUtils.showShort("请先选择锻炼部位")
            }
        }


        holder.setText(R.id.textView, item.dictLabel)

        if (TextUtils.isEmpty(item.trainTime) || item.trainTime == "0") {
            holder.setText(R.id.textView1,"设置运动时间 >>")
        } else {
            holder.setText(R.id.textView1,"运动时间: ${item.trainTime}分钟 >>")
        }

        if (item.isSelect) {
            holder.setImageResource(R.id.imageView1, context.getResId(
                "drawable", item.dictValue.lowercase(Locale.getDefault()) + "_on"))
            holder.getView<View>(R.id.textView1).isVisible = true
        } else {
            holder.setImageResource(R.id.imageView1, context.getResId(
                "drawable", item.dictValue.lowercase(Locale.getDefault()) + "_off"))
            holder.getView<View>(R.id.textView1).isVisible = false

            // 消除已选择项
            item.weeks.forEach{
                it.isSelect = false
            }
            weekListAdapter?.notifyDataSetChanged()
        }

    }

    private var mOnNestChildClickListener: OnNestChildClickListener? = null

    interface OnNestChildClickListener {
        fun onItemClickListener(pos: Int, list: List<DictType?>)
    }

    fun setOnNestChildClickListener(onNestChildClickListener: OnNestChildClickListener ) {
        mOnNestChildClickListener = onNestChildClickListener
    }
}


class WeekListAdapter : BaseQuickAdapter<DictType, BaseViewHolder>(R.layout.item_week) {

    override fun convert(holder: BaseViewHolder, item: DictType) {

        if (item.isSelect) {
            holder.setTextColor(R.id.tv_week, Color.parseColor("#FFFFFF"))
            holder.setBackgroundResource(R.id.tv_week, R.drawable.shape_textview_bg3)
            holder.setText(R.id.tv_week, item.dictLabel)
        } else {
            holder.setTextColor(R.id.tv_week, Color.parseColor("#999999"));
            holder.setBackgroundResource(R.id.tv_week, R.drawable.shape_textview_bg5)
            holder.setText(R.id.tv_week, item.dictLabel)
        }

    }

}